package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.ToIntFunction;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Resource
    private ReportMapper reportMapper;
    //用户统计接口
    @Override
    public UserReportVO  userReport(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dayList = new ArrayList<>();
        dayList.add(begin);
        while (!begin.equals(end)) {
            begin=begin.plusDays(1);
            dayList.add(begin);
        }
        log.error("dayList: {}", dayList.toString());
        ArrayList<Double> newUserCount = new ArrayList<>();
        ArrayList<Double> AllUserCount = new ArrayList<>();

        for (LocalDate localDate : dayList) {
            LocalDateTime localDateStart = LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime localDateend = LocalDateTime.of(localDate,LocalTime.MAX);
            log.error("localDateStart: {}",localDateStart );
            log.error("localDateend: {}",localDateend );
            Double newUser = reportMapper.selectUserCount(localDateStart,localDateend);
            Double AllUser = reportMapper.selectUserCount(null,localDateend);
            newUserCount.add(newUser);
            AllUserCount.add(AllUser);
        }
        log.error("datList: {}",dayList );
        log.error("allusercount: {}",AllUserCount );
        log.error("newUserCount: {}",newUserCount );
        return UserReportVO.builder().dateList(StringUtils.join(dayList,","))
                .totalUserList(StringUtils.join(AllUserCount,","))
                .newUserList(StringUtils.join(newUserCount,",")).build();
    }
    //营业额统计接口
    @Override
    public TurnoverReportVO turnoverReport(Integer status, LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dayList = new ArrayList<>();
        dayList.add(begin);
        while (!begin.equals(end)) {
            begin=begin.plusDays(1);
            dayList.add(begin);
        }
        ArrayList<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dayList) {
            LocalDateTime localDateStart = LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime localDateend = LocalDateTime.of(localDate,LocalTime.MAX);
            Double turnover = reportMapper.turnoverReport(status,localDateStart,localDateend);
            if (Objects.isNull(turnover))
                turnover=0.0;
            turnoverList.add(turnover);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dayList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }
    //订单统计接口
    @Override
    public OrderReportVO ordersReport(Integer status, LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dayList = new ArrayList<>();
        dayList.add(begin);
        while (!begin.equals(end)) {
            begin=begin.plusDays(1);
            dayList.add(begin);
        }
        ArrayList<Integer> fordersList = new ArrayList<>();
        ArrayList<Integer> allordersList = new ArrayList<>();
        for (LocalDate localDate : dayList) {
            LocalDateTime localDateStart = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime localDateend = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer foders = reportMapper.fordersReport(Orders.COMPLETED,localDateStart,localDateend);
            Integer allorders= reportMapper.fordersReport(null,localDateStart,localDateend);
            fordersList.add(foders);
            allordersList.add(allorders);
        }
        //完成的订单总数
        int sumf = fordersList.stream().mapToInt(Integer::intValue).sum();
        //订单总数
        int sumall = allordersList.stream().mapToInt(Integer::intValue).sum();
        return OrderReportVO.builder().dateList(StringUtils.join(dayList,","))
                .orderCountList(StringUtils.join(allordersList,","))
                .validOrderCountList(StringUtils.join(fordersList,","))
                .totalOrderCount(sumall)//总订单
                .validOrderCount(sumf)//有效订单
                .orderCompletionRate(sumf/1.0/sumall).build();
    }
    //销量排名接口
    @Override
    public SalesTop10ReportVO topReport(Integer completed, LocalDate begin, LocalDate end) {
        LocalDateTime localDateStart = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime localDateend = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> goodsSalesDTOList =reportMapper.topReport(completed,localDateStart,localDateend);
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> numberList = new ArrayList<>();
        goodsSalesDTOList.forEach(new Consumer<GoodsSalesDTO>() {
            @Override
            public void accept(GoodsSalesDTO goodsSalesDTO) {
                String name = goodsSalesDTO.getName();
                Integer number = goodsSalesDTO.getNumber();
                nameList.add(name);
                numberList.add(number);
            }
        });
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }
}
